package com.ruoyi.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.domain.bo.SchedulingBo;
import com.ruoyi.demo.domain.vo.SchedulingVo;
import com.ruoyi.demo.domain.Scheduling;
import com.ruoyi.demo.mapper.SchedulingMapper;
import com.ruoyi.demo.service.ISchedulingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 节目排期Service业务层处理
 *
 * @author haha
 * @date 2023-03-22
 */
@RequiredArgsConstructor
@Service
public class SchedulingServiceImpl implements ISchedulingService {

    private final SchedulingMapper baseMapper;

    /**
     * 查询节目排期
     */
    @Override
    public SchedulingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询节目排期列表
     */
    @Override
    public TableDataInfo<SchedulingVo> queryPageList(SchedulingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Scheduling> lqw = buildQueryWrapper(bo);
        Page<SchedulingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询节目排期列表
     */
    @Override
    public List<SchedulingVo> queryList(SchedulingBo bo) {
        LambdaQueryWrapper<Scheduling> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Scheduling> buildQueryWrapper(SchedulingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Scheduling> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, Scheduling::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, Scheduling::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Scheduling::getName, bo.getName());
        lqw.eq(bo.getDays() != null, Scheduling::getDays, bo.getDays());
        lqw.eq(bo.getStartTime() != null, Scheduling::getStartTime, bo.getStartTime());
        lqw.eq(bo.getDuration() != null, Scheduling::getDuration, bo.getDuration());
        lqw.eq(bo.getEndTime() != null, Scheduling::getEndTime, bo.getEndTime());
        return lqw;
    }

    /**
     * 新增节目排期
     */
    @Override
    public Boolean insertByBo(SchedulingBo bo) {
        Scheduling add = BeanUtil.toBean(bo, Scheduling.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改节目排期
     */
    @Override
    public Boolean updateByBo(SchedulingBo bo) {
        Scheduling update = BeanUtil.toBean(bo, Scheduling.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Scheduling entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除节目排期
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
